package net.office.controller;

import net.office.bean.StaffInfo;
import net.office.bean.StaffMail;
import net.office.service.LoginService;
import net.office.service.StaffMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StaffMailController {

    @Autowired
    StaffMailService staffMailService;

    @Autowired
    LoginService loginService;

    /*查询列表*/
    @RequestMapping("/staffMail/list.do")
    public String list(Model model){

        List<StaffMail> staffMailList = staffMailService.findAll();
        model.addAttribute("staffMailList", staffMailList);

        return "address";
    }

    /*搜索列表*/
    @RequestMapping("/staffMail/search.do")
    public String search(@RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffDepartment", required = false) String staffDepartment,
                         @RequestParam(value = "staffPost", required = false) String staffPost,
                         HttpSession session, Model model){

        StaffMail staffMail = new StaffMail();

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffMail.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffMail.setStaffName(staffName);
        }
        if(staffDepartment!=null && staffDepartment.trim().length()!=0){
            staffMail.setStaffDepartment(staffDepartment);
        }
        if(staffPost!=null && staffPost.trim().length()!=0){
            staffMail.setStaffPost(staffPost);
        }

        List<StaffMail> staffMailList = staffMailService.findByStaffMail(staffMail);

        model.addAttribute("staffMailList", staffMailList);

        return "address";
    }

    /*新增*/
    @RequestMapping("/staffMail/insert.do")
    public String insert(@RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffDepartment", required = false) String staffDepartment,
                         @RequestParam(value = "staffPost", required = false) String staffPost,
                         @RequestParam(value = "staffMailPhone", required = false) String staffMailPhone,
                         @RequestParam(value = "staffMailEmail", required = false) String staffMailEmail,
                         @RequestParam(value = "staffMailAddr", required = false) String staffMailAddr,
                         HttpSession session, Model model){

        //检查员工是否存在，工号、姓名是否保持一致
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/staffMail/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/staffMail/toAdd.do";
        }

        StaffMail staffMail = new StaffMail();

        staffMail.setStaffId(staffInfo.getStaffId());
        staffMail.setStaffNo(staffNo);
        staffMail.setStaffName(staffName);

        if(staffDepartment!=null && staffDepartment.trim().length()!=0){
            staffMail.setStaffDepartment(staffDepartment);
        }
        if(staffPost!=null && staffPost.trim().length()!=0){
            staffMail.setStaffPost(staffPost);
        }
        if(staffMailPhone!=null && staffMailPhone.trim().length()!=0){
            staffMail.setStaffMailPhone(staffMailPhone);
        }
        if(staffMailEmail!=null && staffMailEmail.trim().length()!=0){
            staffMail.setStaffMailEmail(staffMailEmail);
        }
        if(staffMailAddr!=null && staffMailAddr.trim().length()!=0){
            staffMail.setStaffMailAddr(staffMailAddr);
        }

        int res = staffMailService.insert(staffMail);
        if(res > 0){
            return "redirect:/staffMail/list.do";
        }

        return "address-add";
    }

    /*修改*/
    @RequestMapping("/staffMail/update.do")
    public String update(@RequestParam(value = "staffMailId") Integer staffMailId,
                         @RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffDepartment", required = false) String staffDepartment,
                         @RequestParam(value = "staffPost", required = false) String staffPost,
                         @RequestParam(value = "staffMailPhone", required = false) String staffMailPhone,
                         @RequestParam(value = "staffMailEmail", required = false) String staffMailEmail,
                         @RequestParam(value = "staffMailAddr", required = false) String staffMailAddr,
                         HttpSession session, Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/staffMail/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/staffMail/toAdd.do";
        }

        StaffMail staffMail = new StaffMail();
        staffMail.setStaffMailId(staffMailId);

        staffMail.setStaffId(staffInfo.getStaffId());
        staffMail.setStaffNo(staffNo);
        staffMail.setStaffName(staffName);

        if(staffDepartment!=null && staffDepartment.trim().length()!=0){
            staffMail.setStaffDepartment(staffDepartment);
        }
        if(staffPost!=null && staffPost.trim().length()!=0){
            staffMail.setStaffPost(staffPost);
        }
        if(staffMailPhone!=null && staffMailPhone.trim().length()!=0){
            staffMail.setStaffMailPhone(staffMailPhone);
        }
        if(staffMailEmail!=null && staffMailEmail.trim().length()!=0){
            staffMail.setStaffMailEmail(staffMailEmail);
        }
        if(staffMailAddr!=null && staffMailAddr.trim().length()!=0){
            staffMail.setStaffMailAddr(staffMailAddr);
        }

        int res = staffMailService.updateByPrimaryKey(staffMail);

        if(res > 0){

        }

        return "redirect:/staffMail/toEdit.do?staffMailId="+staffMailId+"";
    }

    /*删除单条(ajax)*/
    @RequestMapping("/staffMail/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("staffMailId") Integer staffMailId){

        int res = staffMailService.deleteByPrimaryKey(staffMailId);

        Map<String, String> map = new HashMap<String, String>();
        if(res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/staffMail/toAdd.do")
    public String toAdd(){
        return "address-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/staffMail/toEdit.do")
    public String toEdit(@RequestParam("staffMailId") Integer staffMailId, Model model){
        StaffMail staffMail = staffMailService.findOneByStaffMailId(staffMailId);
        model.addAttribute("staffMail", staffMail);

        return "address-edit";
    }
}
